//CouponCoverView.m
//  EasyToo
//
//  Created by Jr Lan on 13-8-30.
//
//

#import "CouponCoverView.h"
#import "UIImageView+OnlineImage.h"


@implementation CouponCoverView
{
    id<CouponCoverViewDelegate> _delegate;
}

- (id)initWithFrame:(CGRect)frame andDelegate:(id<CouponCoverViewDelegate>) mydelegate
{
    self = [super initWithFrame:frame];
    if (self) {
        //set up data
        
        _delegate = mydelegate;
        
        self.items = [NSMutableArray array];
        [self init_view];
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */
- (void)init_view
{
    _wrap = YES;
	
	//add background
	[self setBackgroundColor:BackgroundColor];
	
	//create carousel
	_carousel = [[iCarousel alloc] initWithFrame:CGRectMake(40, 40, 240, 240)];
	_carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _carousel.type = iCarouselTypeCoverFlow2;
	_carousel.delegate = self;
	_carousel.dataSource = self;
    
	//add carousel to view
	[self addSubview:_carousel];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

#pragma mark -
#pragma mark UIActionSheet methods

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex >= 0)
    {
        //map button index to carousel type
        iCarouselType type = buttonIndex;
        
        //carousel can smoothly animate between types
        [UIView beginAnimations:nil context:nil];
        _carousel.type = type;
        [UIView commitAnimations];
    }
}

#pragma mark -
#pragma mark iCarouselDataSource

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [self.couponArray count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    //UIImageView *imgview  = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"0.jpg"]]autorelease];
    Merchant *couponinfo = [self.couponArray objectAtIndex:index];
    
    if (view == nil)
    {
        view = [[CouponCoverItemView alloc] initWithFrame:CGRectMake(0, 0, 160.0f, 250.0f)];
        view.contentMode = UIViewContentModeCenter;
    }
    
    [((CouponCoverItemView*)view) setMovieImageUrl:couponinfo.imageUrl Name:couponinfo.address];
    
    return view;
}

#pragma mark iCarouselDelegate

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    [_delegate coverDetailClick:[self.couponArray objectAtIndex:index]];
}

- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * carousel.itemWidth);
}

- (CGFloat) carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return _wrap;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.05f;
        }
        case iCarouselOptionFadeMax:
        {
            if (carousel.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera
                return 0.0f;
            }
            return value;
        }
        default:
        {
            return value;
        }
    }
}

@end

@implementation CouponCoverItemView

- (id)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self)
    {
        self.imageView  = [[[UIImageView alloc]init] autorelease];
        self.imageView.frame = CGRectMake(0, 0, 160.0f, 200.0f);
        
        [self addSubview:self.imageView];
        
        self.labelView = [[[UILabel alloc]initWithFrame:CGRectMake(0, 200, 160, 30)] autorelease];
        self.labelView.font = [UIFont systemFontOfSize:15];
        self.labelView.lineBreakMode = UILineBreakModeWordWrap;
        self.labelView.numberOfLines = 0;
        self.labelView.textAlignment = NSTextAlignmentCenter;
        self.labelView.backgroundColor = [UIColor clearColor];
        self.labelView.adjustsFontSizeToFitWidth = YES;
        
        [self addSubview:self.labelView];
    }
    return self;
}

- (void)dealloc
{
    self.imageView = nil;
    self.labelView = nil;
    [super dealloc];
}

- (void)setMovieImageUrl:(NSString*)url Name:(NSString*)name
{
    [self.imageView setOnlineImage:url];
    self.labelView.text = name;
}

@end
